#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
int a[N];
int get_k(ll& n, ll p) {
  int cnt = 0;
  while (n % p == 0) n /= p, cnt++;
  return cnt;
}
ll a0, a1, b0, b1;
int get_cnt(ll p) {
  int x = get_k(a0, p);
  int y = get_k(a1, p);
  int z = get_k(b0, p);
  int w = get_k(b1, p);
  int cnt = 0;
  rep(u, y, w) {
    if (min(x, u) == y && max(z, u) == w) cnt++;
  }
  return cnt;
}
void solve() {
  cin >> a0 >> a1 >> b0 >> b1;
  ll ans = 1;
  for (ll i = 2; i * i <= b1; i++) {
    if (b1 % i) continue;
    ans *= get_cnt(i);
  }
  if (b1 > 1) ans *= get_cnt(b1);
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}